package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

import com.ruoyi.common.core.utils.UUIDgen;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.domain.SysUserAddress;
import com.ruoyi.system.domain.ShMarket;
import com.ruoyi.system.domain.ShMarketTj;
import com.ruoyi.system.domain.ShOrderTj;
import com.ruoyi.system.mapper.ShMarketMapper;
import com.ruoyi.system.service.IShProductService;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ShOrderMapper;
import com.ruoyi.system.domain.ShOrder;
import com.ruoyi.system.service.IShOrderService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 订单信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-07-18
 */
@Service
@Transactional
public class ShOrderServiceImpl implements IShOrderService 
{
    @Autowired
    private ShOrderMapper shOrderMapper;

    @Autowired
    private ShMarketMapper shMarketMapper;

    @Autowired
    private ISysDeptService sysDeptService;

    @Autowired
    private IShProductService shProductService;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 查询订单信息
     * 
     * @param id 订单信息主键
     * @return 订单信息
     */
    @Override
    public ShOrder selectShOrderById(Long id)
    {
        return shOrderMapper.selectShOrderById(id);
    }

    /**
     * 查询订单信息列表
     * 
     * @param shOrder 订单信息
     * @return 订单信息
     */
    @Override
    public List<ShOrder> selectShOrderList(ShOrder shOrder)
    {
        List<ShOrder> list = shOrderMapper.selectShOrderList(shOrder);
        HashMap<Long, String> shopMap = sysDeptService.getShopMap();
        HashMap<Long, String> productNameMap = shProductService.getProductMap();
        HashMap<Long, SysUser> userNameMap = sysUserService.getUserMap();
        for (ShOrder obj:list) {
            obj.setShopName(shopMap.get(obj.getShopId()));
            obj.setProductName(productNameMap.get(obj.getProductId()));
            if (null!=userNameMap) {
                SysUser userParam = userNameMap.get(obj.getCreateBy());
                obj.setCreateByName(userParam.getUserName());
                obj.setPhone(userParam.getPhonenumber());
                HashMap<Long, SysUserAddress> addressMap = userParam.getAddressMap();
                SysUserAddress address = addressMap.get(obj.getAddressId());
                if (null!=address) {
                    obj.setReceiver(address.getName());
                    obj.setReceivePhone(address.getPhone());
                    obj.setAddress(address.getAddress());
                }
            }
        }
        return list;
    }

    @Override
    public List<ShOrderTj> selectOrderAmountTjList(ShOrder shOrder)
    {
        return shOrderMapper.selectOrderAmountTjList(shOrder);
    }

    @Override
    public List<ShOrderTj> selectOrderTjList(ShOrder shOrder)
    {
        return shOrderMapper.selectOrderTjList(shOrder);
    }

    @Override
    public List<ShOrderTj> selectOrderPmList(ShOrder shOrder)
    {
        List<ShOrderTj> listTj = shOrderMapper.selectOrderPmList(shOrder);
        HashMap<Long, String> shopMap = sysDeptService.getShopMap();
        for (ShOrderTj obj:listTj) {
            obj.setShopName(shopMap.get(obj.getShopId()));
            ShOrder shOrderParam = new ShOrder();
            shOrderParam.setShopId(obj.getShopId());
            shOrderParam.setCreateAtstart(shOrder.getCreateAtstart());
            shOrderParam.setCreateAtend(shOrder.getCreateAtend());
            List<ShOrderTj> orderTj = shOrderMapper.selectOrderAmountTjList(shOrderParam);
            BigDecimal payAmount = BigDecimal.valueOf(0);
            for (ShOrderTj objtj:orderTj) {
                payAmount = payAmount.add(objtj.getPayAmount());
            }
            ShMarket shMarketParam = new ShMarket();
            shMarketParam.setShopId(obj.getShopId());
            shMarketParam.setCreateAtstart(shOrder.getCreateAtstart());
            shMarketParam.setCreateAtend(shOrder.getCreateAtend());
            List<ShMarketTj> marketTj = shMarketMapper.selectMarketAmountTjPayList(shMarketParam);
            BigDecimal amountMarket = BigDecimal.valueOf(0);
            for (ShMarketTj objtj:marketTj) {
                amountMarket = amountMarket.add(objtj.getMarketAmount());
            }
            obj.setPayAmount(payAmount);
            obj.setMarketAmount(amountMarket);
        }
        return listTj;
    }

    @Override
    public List<ShOrderTj> selectPayPmList(ShOrder shOrder)
    {
        List<ShOrderTj> listTj = shOrderMapper.selectPayPmList(shOrder);
        HashMap<Long, String> shopMap = sysDeptService.getShopMap();
        for (ShOrderTj obj:listTj) {
            obj.setShopName(shopMap.get(obj.getShopId()));
        }
        return listTj;
    }

    /**
     * 新增订单信息
     * 
     * @param shOrder 订单信息
     * @return 结果
     */
    @Override
    public int insertShOrder(ShOrder shOrder)
    {
        shOrder.setOrderNo(UUIDgen.getUUID(20));
        return shOrderMapper.insertShOrder(shOrder);
    }

    /**
     * 修改订单信息
     * 
     * @param shOrder 订单信息
     * @return 结果
     */
    @Override
    public int updateShOrder(ShOrder shOrder)
    {
        return shOrderMapper.updateShOrder(shOrder);
    }

    /**
     * 批量删除订单信息
     * 
     * @param ids 需要删除的订单信息主键
     * @return 结果
     */
    @Override
    public int deleteShOrderByIds(Long[] ids)
    {
        return shOrderMapper.deleteShOrderByIds(ids);
    }

    /**
     * 删除订单信息信息
     * 
     * @param id 订单信息主键
     * @return 结果
     */
    @Override
    public int deleteShOrderById(Long id)
    {
        return shOrderMapper.deleteShOrderById(id);
    }
}
